<template>
	<view>
		<notices :messages="messages" :handleNoticeInfo="handleNoticeInfo">
		</notices>
		<!-- <tui-loadmore v-if="loading"></tui-loadmore> -->
		<tui-nomore v-if="!allowLoading"></tui-nomore> <!--显示没有更多数据-->
	</view>
</template>

<script>
import c from "@/utils/Constant.js"
import noticeParser from "@/utils/NoticeParser.js"
import notices from "@/pages/message/notices.vue";
import messageApi from "@/request/api/messageApi.js";
import communityApi from "@/request/api/communityApi.js";
import listUtils from "@/utils/ListUtils.js"
import userUtils from "@/utils/userUtils.js"
import noticeBuilder from "@/utils/NoticeBuilder.js"
const commentUrl = "#/"  //评论详情的url
const topicUrl = "#/"	 //话题详情的url
	export default {
		data() {
			return {
				messages:[	
				],
				comments:[], //评论集合
				topics:[],    //话题集合
				page:{
					currentPage:0,
					size:10
				},
				allowLoading:true,//是否允许加载，如果数据已经全部拉取完毕，就可以关闭了
			}
		},
		created(){
			this.getSystemMeessage();
			
		},
		methods: {
			//拉取系统发给当前用户的消息
			getSystemMeessage(){
				this.page.currentPage+=1;
				messageApi.historyList(c.systemIds.SYSTEM,this.page).then((data)=>{
					if(data.code ==0){
						let resultList = data.page.list
						this.getEntitys(resultList)
						setTimeout(()=>{  //这里慢点合并消息，否则会出现问题（异步加载先后顺序问题，这里暂时用这种low的写法）
							this.messages = [...this.messages,...resultList]
							//console.log(this.messages)
						},50);	
						if(resultList.length < this.page.size){//如果查出来的记录数小于要求的记录数，说明已经没有记录拉取了
							this.allowLoading = false; //不允许继续拉取记录
						}
					}	
				})
				
			},
			//拉取实体
			getEntitys(messages){	
				let commentsIds = []
				let topicIds = []
				for(let message of messages){
					let msgCon = JSON.parse(message.content);
					if(msgCon.entityId){//有实体id。才进行拉取
						if(msgCon.entityType == c.entityType.TOPIC){
							topicIds.push(msgCon.entityId)
						}else if(msgCon.entityType == c.entityType.COMMENT){
							commentsIds.push(msgCon.entityId)					
						}
					}
				}
				//拉取多个话题
				communityApi.batchTopicInfo(listUtils.removeRepeat(topicIds)).then((data)=>{
					if(data.code == 0 && data.data){
						this.topics = [...this.topics,...data.data]
					}
				})
				
				//拉取多个评论
				communityApi.batchCommentInfo(listUtils.removeRepeat(commentsIds)).then((data)=>{
					if(data.code == 0 && data.data){
						this.comments = [...this.comments,...data.data]
					}
				})
				
			},
			
			
			//处理通知的逻辑，由通知组件来调用
			handleNoticeInfo(message,trigger){
				let msgCon = JSON.parse(message.content);
				console.log(msgCon)
				if(message.type == c.action.NO_SPEAK || message.type == c.action.NORMAL_SPEAK){
					return this.buildNoSpeakNotices(message);
				}else if(msgCon.entityType == c.entityType.TOPIC ){//实体类型为话题
					return this.buildTopicNotice(message)
				}else if(msgCon.entityType == c.entityType.COMMENT ){//实体类型为评论
					return this.buildCommentNotice(message)
				}
				
			},
			
			//构建禁言类型的通知
			buildNoSpeakNotices(message){
				let notice = {
					title:'系统通知',
					content:'',
					entity:'',
					jumpUrl:''
				}
				notice.content = noticeParser.parseContent(message);
				return notice;
			},
			buildTopicNotice(message){
				//通过解析器获取消息对应的通知内容
				let content = noticeParser.parseContent(message);
				//构建通知对象
				return noticeBuilder.buildTopicNotice(message,this.topics,"系统通知",content)	 
			},
			
			//构建实体类型为评论的系统通知对象
			buildCommentNotice(message){
				//通过解析器获取消息对应的通知内容
				let content = noticeParser.parseContent(message);
				//构建通知对象
				return noticeBuilder.buildCommentNotice(message,this.comments,"系统通知",content)	
			},
		},
		components:{
			notices
		},
		// 页面上拉触底事件的处理函数
		onReachBottom: function() {
			if(this.allowLoading){
				this.getSystemMeessage();
			}
		}
	}
</script>


<style>
</style>
